﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Data;

namespace UIElements.ViewModel
{
    public class TreeViewItemViewModel : ViewModelBase
    {
        #region Constructors

        protected TreeViewItemViewModel(TreeViewItemViewModel parent)
        {
            this.parent = parent;
            this.children = new ObservableCollection<TreeViewItemViewModel>();
        }

        protected TreeViewItemViewModel()
        {
        }

        #endregion

        #region Properties

        private string previousName = string.Empty;
        public string PreviousName
        {
            get { return previousName; }
            set 
            { 
                previousName = value;
                OnPropertyChanged("PreviousName");
            }
        }

        private bool isInEditMode;
        public bool IsInEditMode
        {
            get { return isInEditMode; }
            set
            {
                isInEditMode = value;
                OnPropertyChanged("IsInEditMode");
            }
        }

        /// <summary>
        /// Name of the treeview item
        /// </summary>
        private string name;
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }

        private ObservableCollection<TreeViewItemViewModel> children = new ObservableCollection<TreeViewItemViewModel>();
        public ObservableCollection<TreeViewItemViewModel> Children
        {
            get { return children; }
            set
            {
                children = value;
                OnPropertyChanged("Children");
            }
        }

        private TreeViewItemViewModel parent;
        public TreeViewItemViewModel Parent
        {
            get { return parent; }
            set
            {
                parent = value;
                OnPropertyChanged("Parent");
            }
        }

        private bool isExpanded;
        public bool IsExpanded
        {
            get { return isExpanded; }
            set
            {
                if (value != isExpanded)
                {
                    isExpanded = value;
                    this.OnPropertyChanged("IsExpanded");
                }

                // Expand all the way up to the root.
                if (isExpanded && parent != null)
                    parent.IsExpanded = true;
            }
        }

        private bool isSelected;
        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                if (value != isSelected)
                {
                    isSelected = value;
                    this.OnPropertyChanged("IsSelected");
                }
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// to be overrided in derived class
        /// </summary>
        /// <returns></returns>
        public virtual DataTable GetProperties()
        {
            return new DataTable();
        }

        public virtual bool SaveChanges()
        {
            return true;
        }

        #endregion
    }
}
